using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Linq;
using Mpi.Common;
using Mpi.Common.Collections;
using Xeml.Document.Contracts;
using Xeml.Document.Manager;
using Xeml.Document.Ontologies;
using System.Text.RegularExpressions;
using Xeml.Document.EnvironmentalOntology;

namespace Xeml.Document.Ontologies.OBO
{
    /// <summary>
    /// this is the handler for the Plant Environment Ontology
    /// </summary>
    public class PEOHandler : ContextLessHandler, IDynamicOntologyHandler
    {
        public static readonly string URI = "http://xeml.mpimp-golm.mpg.de/xemleo";

        #region constructors
        public PEOHandler()
            : base()
        {
            //InstanceLocation = @"D:\jahu\Visual Studio 2010\Xeml\XEML\EnvironmentOntology_v1_6.obo";
            InstanceLocation = @"http://obo.cvs.sourceforge.net/viewvc/obo/obo/ontology/phenotype/environment/environment_ontology.obo";
        }

        public PEOHandler(PEOHandler other)
            : base()
        {
            InstanceLocation = other.InstanceLocation;
        }
        #endregion constructors

        #region IXemlComponent Members
        public string Publisher
        {
            get { return "Max Planck Institute of Molecular Plant Physiology, Potsdam/Golm, Germany"; }
        }

        private Version _version = new Version(1, 0, 0, 0);
        public Version Version
        {
            get { return _version; }
        }

        public new string ComponentName
        {
            get { return "Plant Environment Ontology (PEO) Handler"; }
        }

        public string Author
        {
            get { return "Jan Hummel"; }
        }

        public string Description
        {
            get { return "It represents a controlled vocabulary to describe different types of supplemental environments that have been reported in the experimental profiles of gene expression and phenotype (mutant and QTL) studies on cereal plants."; }
        }

        public string Uri
        {
            get { return URI; }
        }

        public IXemlComponent Copy()
        {
            return new PEOHandler(this);
        }
        #endregion

        protected override void DoTagTypeAnnotation(ParsedOboFile collection)
        {
            List<string> _environmentTerms = new List<string>();

            bool altered = true;
            while (altered)
            {
                altered = false;
                foreach (var item in collection.AsParallel().AsUnordered().Where(x => x.StanzaType == OboStanzaTypes.Term && x.isNoneTerm))
                {
                    if (item.Any(x => x.Item1 == OboTags.is_a))
                    {
                        foreach (var is_a in item.Where(x => x.Item1 == OboTags.is_a))
                        {
                            int index = _environmentTerms.BinarySearch(is_a.Item2);
                            if (index >= 0)
                            {
                                index = _environmentTerms.BinarySearch(item.Id);
                                if (index < 0)
                                {
                                    item.HeaderClass = OboTermClass.EnvironmentVariable;
                                    _environmentTerms.Insert(~index, item.Id);
                                }
                                altered = true;
                            }
                        }
                    }
                    else
                    {
                        if (item.Name == "plant environment ontology")
                        {
                            _environmentTerms.Insert(0, item.Id);
                            item.HeaderClass = OboTermClass.EnvironmentVariable;
                            altered = true;
                        }
                    }
                }
            }
        }
    }
}
